﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FluentValidation.Validators;

namespace Utilities.Validation.ExtensionMethods
{
    public class UserNameValidator : PropertyValidator, IRegularExpressionValidator
    {
        private readonly Regex regex;
        const string expression = @"^[a-zA-Z0-9_-]{4,16}$";

        public UserNameValidator()
            : base(() => ZHResources.username_error)
        {
            regex = new Regex(expression, RegexOptions.IgnoreCase);
        }


        protected override bool IsValid(PropertyValidatorContext context)
        {
            if (context.PropertyValue == null) return true;

            if (!regex.IsMatch((string)context.PropertyValue))
            {
                return false;
            }

            return true;
        }

        public string Expression
        {
            get { return expression; }
        }
    }

}
